package com.heima.behavior.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.heima.behavior.service.UnlLikeBehaviorService;
import com.heima.common.rides.RedisCacheService;
import com.heima.model.behavior.dtos.UnLikeBehaviorDto;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.user.pojos.ApUser;
import com.heima.utils.threadlocal.AppThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UnlLikeBehaviorServiceImpl implements UnlLikeBehaviorService {

    @Autowired
    private RedisCacheService redisCacheService;

    @Override
    public ResponseResult unLikeBehavior(UnLikeBehaviorDto dto) {
        //1.参数校验
        if (dto == null || dto.getArticleId() == null || dto.getType() < 0 || dto.getType() > 1) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //2.判断登录用户
        ApUser user = AppThreadLocalUtil.getUser();
        if (user == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //3.判断不喜欢还是取消不喜欢
        if (dto.getType()==0){
            //4.不喜欢,存数据到redis
            redisCacheService.hPut("BEHAVIOR_UNLIKE_"+dto.getArticleId(),user.getId().toString(), JSON.toJSONString(dto));
        }else {
            //5.取消不喜欢,删除redis中数据
            redisCacheService.hDelete("BEHAVIOR_UNLIKE_"+dto.getArticleId(),user.getId().toString());
        }

        return ResponseResult.okResult(null);
    }
}
